﻿/// ****************************************************************************
/// Author:	        Robin Zhu
/// Create Date:	2012-4-26
/// Purpose: 		应用程序入口设置集合
/// ****************************************************************************
/// Modify By		Date			Remark
/// Robin           2013-1-24       修改Remove方法定义
/// ****************************************************************************
/// 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RB.Domain.RelationalMapping;
using RB.DataAccess;

namespace RB.Web.Security.Rdb
{
    /// <summary>
    /// 应用程序入口设置集合
    /// </summary>
    public class AppEntrySet : CachedKeyedRecordSet<AppEntry, string>
    {
        public IQueryable<AppEntry> WhereByUser(Guid userId)
        {
            using(Executor exec = DataContext.CreateExecutor())
            {
                var q = from p in exec.Query<AppEntry>()
                        join p1 in exec.Query<UserAppEntry>() on p.Code equals p1.AppCode
                        where p1.UserId == userId
                        orderby p.Sequence
                        select p;
                return q;
            }
        }

        public AppEntry SingleForUser(string appCode, Guid userId)
        {
            using (Executor exec = DataContext.CreateExecutor())
            {
                var q = from p in exec.Query<AppEntry>()
                        join p1 in exec.Query<UserAppEntry>() on p.Code equals p1.AppCode
                        where p1.UserId == userId && p.Code == appCode
                        orderby p.Sequence
                        select p;
                return q.SingleOrDefault();
            }
        }

        //#region 实现KeyedRecordSet

        //public override bool Contains(AppEntry item)
        //{
        //    return this.Any(p => p.Code == item.Code);
        //}

        //public override bool Remove(AppEntry item, Executor exec)
        //{
        //    int count = this.RemoveAll(p => p.Code == item.Code, exec);
        //    return count > 0;
        //}

        //public override AppEntry this[string key]
        //{
        //    get
        //    {
        //        return this.AsQueryable().Where(p => p.Code == key).Single();
        //    }
        //    set
        //    {
        //        this.Update(p => value, p => p.Code == key);
        //    }
        //}

        //#endregion 实现KeyedRecordSet

        /// <summary>
        /// 判断一个用户是否允许访问某个应用的公开部分
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        internal bool IsUserAllowed(string appCode, Guid userId)
        {
            var entry = (new UserAppEntrySet()).SingleOrDefault(t => t.AppCode == appCode && t.UserId == userId);
            return entry != null;
        }

        protected override string GetKey(AppEntry item)
        {
            return item.Code;
        }

        protected override AppEntry GetValue(string key)
        {
            return base.SingleOrDefault(p => p.Code == key);
        }

        protected override bool RemoveByKey(string key, Executor exec)
        {
            int n = base.RemoveAll(p => p.Code == key, exec);
            return n > 0;
        }

        protected override void SetValue(string key, AppEntry value)
        {
            base.Update(p => value, p => p.Code == key);
        }

        internal AppEntry[] GetByTicket(string ticket)
        {
            var q = from t in this.AsQueryable()
                    join t1 in (new UserAppEntrySet()).AsQueryable() on t.Code equals t1.AppCode
                    join t2 in (new UserSessionSet()).AsQueryable() on t1.UserId equals t2.UserId
                    where t2.Ticket == ticket && !t.IsDisabled
                    orderby t.Sequence
                    select t;
            return q.ToArray();
        }
    }
}
